/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO.InformationFilter;

import DAO.DAOAccess;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author tran tuan tai
 */
public class CategoryDAO
{

    public static void DoEmpty()
    {
         try
        {
            java.sql.Connection conn = DAOAccess.GetConnection();
            String sql = "DELETE FROM CATEGORY";
            PreparedStatement stat = conn.prepareCall(sql);
            stat.execute();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public int Insert(String bow, String boc)
    {
        int id = -1;
        try
        {
            java.sql.Connection conn = DAOAccess.GetConnection();
            String sql = "INSERT INTO CATEGORY(BOW, BOC) VALUES(?, ?)";
            PreparedStatement statement = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            statement.setString(1, bow);
            statement.setString(2, boc);

            statement.executeUpdate();
            ResultSet result = statement.getGeneratedKeys();
            result.first();
            id = result.getInt(1);
            statement.close();

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return id;
    }

    public List<Integer> GetAllID()
    {
        String query = "SELECT ID FROM CATEGORY";
        List<Integer> result = new ArrayList<Integer>();
        try
        {
            ResultSet rs =  DAOAccess.ExecuteQuery(query);
            rs.beforeFirst();
            while(rs.next())
            {
                result.add(rs.getInt(1));
            }
            rs.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return result;
    }

}
